package com.zhongxiangspace.tools;

import com.zhongxiangspace.enums.ImageSizeTypeEnum;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class FileUtil {
    private static final String Path = FileUtil.class.getResource("/").toString() + "/static/common/tools/watermark.png";
    private static final File WATER_IMG=new File(Path.substring(6, FileUtil.Path.length()));
    /**
     * 上传图片并且处理图片
     * @param multipartFile
     * @param filePath
     * @param prefixPath
     * @return
     */
    public static Map<String, Object> saveFile(MultipartFile multipartFile, String filePath, String prefixPath) {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        if (multipartFile.isEmpty()) {
            return null;
        } else {
            String strPath="/images/" + dateFormater.format(date);
            // 获取文件名+存放的全路径
            String path = prefixPath + strPath;// 存放位置
            File f = new File(path);
            if (f.exists()) {// 判断文件夹是否存在
            } else {
                f.mkdirs();
            }
            Map<String, Object> map = FileUtil.manageImageSave(multipartFile, path,strPath,filePath);
            return map;
        }
    }

    public static Map<String, Object> manageImageSave(MultipartFile multipartFile, String path,String strPath,String filePath) {
        Map<String, Object> map = new HashMap<>();
        String newFileName = "";
        newFileName = FileUtil.updateFileName(multipartFile.getOriginalFilename());
        String originalPath = path + "/" + newFileName;
        map.put("originalPath",strPath+"/" + newFileName);
        String maxPath = path + "/" + ImageSizeTypeEnum.SIZE_MAX.getName() + newFileName;
        map.put("maxPath",strPath+"/" + ImageSizeTypeEnum.SIZE_MAX.getName() + newFileName);
        String minPath = path + "/" + ImageSizeTypeEnum.SIZE_MIN.getName() + newFileName;
        map.put("minPath", strPath+"/" + ImageSizeTypeEnum.SIZE_MIN.getName() + newFileName);
        try {
            BufferedImage srcImage = null;
            FileInputStream in = (FileInputStream) multipartFile.getInputStream();
            srcImage = javax.imageio.ImageIO.read(in);
            int height_max = FileUtil.newHeight(srcImage.getWidth(), srcImage.getHeight(), ImageSizeTypeEnum.SIZE_MAX.getWidth());
            int height_min = FileUtil.newHeight(srcImage.getWidth(), srcImage.getHeight(), ImageSizeTypeEnum.SIZE_MIN.getWidth());
            //原图上传
            multipartFile.transferTo(new File(originalPath));
            //max 大图
            Thumbnails.of(originalPath).size(ImageSizeTypeEnum.SIZE_MAX.getWidth(), height_max).toFile(maxPath);
            //min小图
            Thumbnails.of(originalPath).size(ImageSizeTypeEnum.SIZE_MIN.getWidth(), height_min).toFile(minPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 加水印
     * @param targetImg 原图路径
     * @param waterImg 水印图片
     * @param positions 位置
     * @throws IOException
     */
    public final static void pressImage(String targetImg, File waterImg, Positions positions) throws IOException {
        Thumbnails.of(targetImg)
                .watermark(positions, ImageIO.read(waterImg), 0.20f)
                .scale(2)//缩放比例
                .toFile(targetImg);
    }

    public static int newHeight(int originalWidth, int originalHeight, int newWidth) {
        int height = originalHeight;
        if (originalWidth > newWidth) {
            BigDecimal bigDecimal = new BigDecimal((double) newWidth / originalWidth).setScale(2, BigDecimal.ROUND_HALF_UP);
            height = (int) Math.floor(bigDecimal.doubleValue() * originalHeight);
        }
        return height;
    }

    public static String getFileURL(String fileName, String prefixPath) {
        String path = prefixPath + fileName;// 存放位置
        return path;
    }

    // 修改文件名称
    public static String updateFileName(String name) {
        String[] names = name.split("\\.");
        long l = System.currentTimeMillis();
        return l + "." + names[names.length - 1];
    }

    // 获得文件路径
    public static String returnFileName(String prefixPath, String filePath) {
        String path = prefixPath;
        path = prefixPath + filePath;
        return path;
    }

    // 删除文件
    public static boolean deleteFile(String file) {
        File f = new File(file);
        if (f.exists()) {
            f.delete();
            return true;
        }
        return false;
    }


}
